package com.example.jbmsuppliers.sinopec.orderaddress;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.example.jbmsuppliers.R;
import com.example.jbmsuppliers.sinopec.activity.ActivityUse;
import com.example.jbmsuppliers.sinopec.net.NetAddRess;
import com.example.jbmsuppliers.sinopec.view.DialogLoading;
import com.jbm.jbmsupplier.util.IOrder;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import static com.example.jbmsuppliers.R.id.ra_order1;
import static com.example.jbmsuppliers.R.id.ra_order2;


/**
 * Created by 家帮忙 on 2017/2/28.
 */

public class MyOrderEvalua extends ActivityUse implements RatingBar.OnRatingBarChangeListener {

    @InjectView(R.id.header_title)
    TextView headerTitle;
    @InjectView(R.id.header_right)
    TextView headerRight;
    @InjectView(R.id.header_back)
    ImageView headerBack;
    @InjectView(R.id.te_order_neirong_pj)
    EditText teOrderNeirongPj;
    @InjectView(R.id.ra_order)
    RatingBar raOrder;
    @InjectView(R.id.ra_order1)
    RatingBar raOrder1;
    @InjectView(R.id.ra_order2)
    RatingBar raOrder2;
    @InjectView(R.id.bu_order_fb)
    Button buOrderFb;

    private float i, a, c;
    private Float flo;
    private int io;
    private int ao;
    private int co;
    private DialogLoading dialogLoading;
private final int RATING=0001;

    @Override
    protected int setContentView1() {
        return R.layout.order_pinfen_layout;
    }

    @Override
    protected void initView() {
        ButterKnife.inject(this);

        initview();
    }

    private void initview() {
        headerTitle.setText("评价");
        headerRight.setVisibility(View.GONE);
        raOrder.setOnRatingBarChangeListener(this);
        raOrder1.setOnRatingBarChangeListener(this);
        raOrder2.setOnRatingBarChangeListener(this);
        findViewById(R.id.bu_order_fb).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!teOrderNeirongPj.getText().toString().equals("") || teOrderNeirongPj.length() < 0) {
                    NetAddRess netAddRess = new NetAddRess(handler);
                    netAddRess.getRating(getIntent().getStringExtra("id"), teOrderNeirongPj.getText().toString(), io, ao, co, RATING);
                    dialogLoading = new DialogLoading(MyOrderEvalua.this, "加载中...", true);
                    dialogLoading.show();
                } else {
                    hint("内容不能为空");

                }
            }
        });
    }

    @Override
    public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
        switch (ratingBar.getId()) {
            case R.id.ra_order:
                i = ratingBar.getRating();
                Float flo = new Float(i);
                io = flo.intValue();
                break;
            case ra_order1:
                a = ratingBar.getRating();
                Float floo = new Float(a);
                ao = floo.intValue();
                break;
            case ra_order2:
                c = ratingBar.getRating();
                Float flooo = new Float(c);
                co = flooo.intValue();
                break;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case RATING: {
                    IOrder rating_order = (IOrder) msg.obj;
                    if (rating_order.back) {
                        if (dialogLoading != null)
                            dialogLoading.dismiss();
                        finish();
                    } else {
                        if (dialogLoading != null)
                            dialogLoading.dismiss();
                        hint(rating_order.errorinfo);
                    }

                    break;
                }
            }
            super.handleMessage(msg);
        }
    };

    @OnClick(R.id.header_back)
    public void onClick() {
        finish();
    }


}
